package cn.spark.study.sql

import org.apache.spark.sql.SQLContext
import org.apache.spark.SparkContext
import org.apache.spark.sql.hive.HiveContext

object HiveDataSource {
  def main(args: Array[String]): Unit = {
   val sc = new ContextUtil().getContext("HiveDataSource")
   val sqlC = new SQLContext(sc)
   hiveDataSource(sc,sqlC)
   
  }
  
  def hiveDataSource(sc:SparkContext,sqlC:SQLContext):Unit={
    val hiveC = new HiveContext(sc);
    
    hiveC.sql("DROP TABLE IF EXISTS student_infos");
    hiveC.sql("CREATE TABLE IF NOT EXISTS student_infos(name STRING, age INT)");
    hiveC.sql("LOAD DATA LOCAL INPATH '/usr/local/spark-study/resource/student_infos.txt' INTO TABLE student_infos");
    
    hiveC.sql("DROP TABLE IF EXISTS student_scores");
    hiveC.sql("CREATE TABLE IF NOT EXISTS student_scores(name STRING, score INT)");
    hiveC.sql("LOAD DATA LOCAL INPATH '/usr/local/spark-study/resource/student_scores.txt' INTO TABLE student_scores"); 
    
    hiveC.sql("DROP TABLE IF EXISTS good_student_info");
    val goodStudentDF = hiveC.sql("select si.name,si.age,ss.score from student_infos si join student_scores ss on si.name = ss.name where ss.score >=80")
     
    goodStudentDF.saveAsTable("good_student_info")
    
    hiveC.table("good_student_info").show()
    
  }
}